package com.mactivity.helper;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * This class is a wrapper for accessing the shared preferences available in
 * Android. It creates a single shared preference for the entire application
 * which will be used in various activities.
 * 
 * @author Shwetha Narayan
 * 
 */
public class SharedPreferenceHelper {
	private SharedPreferences sharedPreferenceSetting;
	private Context context;

	/**
	 * Constructor of the class. Creates the Shared Preference.
	 * 
	 * @param context
	 *            The application view.
	 */
	public SharedPreferenceHelper(Context context) {
		this.context = context;
		this.sharedPreferenceSetting = PreferenceManager
				.getDefaultSharedPreferences(this.context);
	}

	/**
	 * The getter method returning the ShareadPreference object.
	 * 
	 * @return ShareadPrerence
	 */
	public SharedPreferences getSharedPreferenceSetting() {
		return sharedPreferenceSetting;
	}

	/**
	 * Creates a Map<String, String> of the details of the User currently logged
	 * in.
	 * 
	 * @return Map<String, String> : Containing all the user information if
	 *         there a logged in User. null : if there is no logged in user.
	 */
	public Map<String, String> getUserInformation() {
		Map<String, String> userInfoMap = null;

		if (getSharedPreferenceSetting().getBoolean(Constants.IS_LOGGED_IN,
				false)) {
			userInfoMap = new HashMap<String, String>();
			userInfoMap.put(Constants.USER_ID, String.valueOf(getSharedPreferenceSetting()
					.getInt(Constants.USER_ID, 0)));
			userInfoMap.put(Constants.USER_NAME, getSharedPreferenceSetting()
					.getString(Constants.USER_NAME, null));
			userInfoMap.put(Constants.USER_EMAIL_ID, getSharedPreferenceSetting()
					.getString(Constants.USER_EMAIL_ID, null));
		}

		return userInfoMap;
	}
}
